;-------------------------------
;      POCETAK APLIKACIJE       |
;-------------------------------

RES_TAB SEGMENT AT OFFFF0h
	JMP Far ptr START
RES_TAB ENDS;




;-------------------------------------------
;		DATA SEGMENT, NAJNIZE ADRESE		|
;-------------------------------------------
DAT SEGMENT AT 000000h

;-----------------------------------------------
;		IVT INICIJALIZACIJA, PROMENLJIVE		|
;-----------------------------------------------

INT_DIV_ZERO	DW	2		DUP(?)	; ULAZ 0, DELJENJE NULOM, DVE RECI 
INT_TRAP		DW	2		DUP(?)	; TRAP
INT_NMI			DW	2		DUP(?)	; NEMASKIRAJUCI PREKID
INT_TABLE		DW	2 * 253	DUP(?)	; OSTATAK IVT-a (UKUPNO 256 ULAZA)

;ADRESE PREKIDNIH RUTINA VEZANIH NA 8259a
INT_CNT_1MS		EQU		4 * 20h	; PREKID NA SVAKU ms, ULAZ 0 8259a (SIZE * ICW2)
INT_KEYBOARD	EQU		4 * 21h	; PREKID USLED PRITISNUTOG TASTERA
INT_CNT_20MS	EQU		4 * 22h	; PREKID 20ms
;	23h NE KORISTI SE
;	24h NE KORISTI SE
;	25h NE KORISTI SE
INT_PAPER		EQU     4 * 26h ; PREKID OD STAMPACA
INT_SERIAL		EQU		4 * 27h	; PREKID OD SERIJSKOG PORTA


;------------------------------------
;			STANJA SISTEMA			 |
;------------------------------------

STATE_CEKAM					EQU 1
STATE_PROGRAMIRANJE			EQU 2
STATE_SPREMAN				EQU 3
STATE_UNOS_STAVKI			EQU 4
STATE_UNOS_KOLICINE         EQU 5
STATE_RACUN					EQU 6
STATE FUNKCIJA				EQU 7
STATE_ERROR					EQU 8



CURRENT_STATE	db 1
PAST_STATE db 1  ; potrebno za oporavak od greske
ERROR_OCCURED	db 1 DUP(0)




;------------------------------------
;			SERIJSKI PORT 8251		 |
;------------------------------------

;ADRESE PERIFERIJE

ADR_8251_CTRL EQU 0FF00h
ADR_8251_DATA EQU 0FF02h

;PODESAVANJE MODA
;USVOJENA PODESAVANJA:
;BAUD RATE FACTOR = 16
;CHARACTER LENGTH = 8
;BROJ STOP BITA = 2

MODE_8251 EQU 11111101b



;KOMANDNA REC PRI INICIJALIZACIJI
;ISKLJUCEN HUNT MODE
;NORMALAN REZIM RADA
;/RTS = 0
;RESETOVANI ERROR FLAGOVI
;NORMALAN REZIM RADA
;DOZVOLJEN PRIJEM
;/DTR = 0
;DOZVOLJENO SLANJE

COMMAND_8251_STANDARD_INIT EQU 00110111b


;KOMANDA ZA RESET

COMMAND_8251_RESET EQU 40h

;MOD RADA

SERIAL_MODE_SEND EQU 1
SERIAL_MODE_RCV  EQU 2

CURR_SERIAL_MODE db 1

SERIAL_OK_TO_SEND db 1
SERIAL_NEW_RCV db 0

SERIAL_SND_CNT db 0

;-------------------------------------------
;   PARALELNI PORTA 8255                    |
;   PORT A - STAMPAC, PORT B - TASTAURA		|
;	PORT C - TASTATURA						|
;-------------------------------------------

;ADRESE PERIFERIJE

ADR_8255_PRINTER EQU 0FF10h    
ADR_8255_KEYOUT EQU 0FF12h          
ADR_8255_KEYIN EQU 0FF14h           
ADR_8255_PRINTER_KEY_CTRL EQU 0FF16h      

;KOMANDNA REC
;SVI PORTOVI U MOD0
;PORTOVI A i C IZLAZNI
;PORT B ULAZNI

CTRL_8255_INIT_PRINTER_KEY EQU 10000001b




;-----------------------------------------------
;		PARALELNI PORTA 8255					|
;		PORT A - DISPLEJ, PORT B - DISPLEJ		|
;-----------------------------------------------

;ADRESE PERIFERIJE
ADR_8255_DISPLAY_CHAR		EQU 0FF20h 			
ADR_8255_DISPLAY_SELECT	 	EQU 0FF22h 			
ADR_8255_DISPLAY_C		 	EQU 0FF24h				
ADR_8255_DISPLAY_CTRL		EQU 0FF26h			

;KOMANDNA REC
;PORTOVI A I B IZLAZNI
CTRL_8255_INIT_DISPLAY		EQU	10000000b	


;-----------------------------------
;		KONTROLER PREKIDA 8259a		|
;-----------------------------------

;ADRESE PERIFERIJE

ADR_8259_DATA	EQU 0FF30h		
ADR_8259_CTRL	EQU 0FF32h			

;SAMO JEDAN 8259a U SISTEMU, OKIDANJE NA IVICU, POTREBNA ICW4
ICW1_8259	EQU 00010111b

;ULAZI U IVT DODELJENI KONTROLORU PREKIDA 20h - 27h 
ICW2_8259	EQU 20h

;AUTO EOI
ICW4_8259 	EQU 00000011b



;-----------------------
;		BROJAC 8254		|
;-----------------------

;ADRESE PERIFERIJE

ADR_CNT_DATA_0		EQU 0FF40h		
ADR_CNT_DATA_1		EQU 0FF42h	
ADR_CNT_DATA_2		EQU 0FF44h	
ADR_CNT_CTRL		EQU 0FF46h	

; PRVI BROJAC RADI U MODU 3 I OKIDA NA 1ms 
COUNTER_0_CTRL				EQU 00110110b
COUNTER_0_START_VALUE_L		EQU	CDh  ;POCETNA VREDNOST 1CCDh = 7373 (NA 5MHz ~ 1ms)
COUNTER_0_START_VALUE_H		EQU	1Ch
	
; DRUGI BROJAC RADI U MODU 3 I OKIDA NA 20ms
COUNTER_1_CTRL				EQU 01110110b
COUNTER_1_START_VALUE_L		EQU	14h  ;14h = 20
COUNTER_1_START_VALUE_H		EQU	00h
	
	
; TRECI BROJAC RADI U MODU 3 I DAJE TAKT SERIJSKOM PORTU
COUNTER_2_CTRL				EQU 10110110b 
COUNTER_2_START_VALUE_L		EQU	48h  ;48H = 72
COUNTER_2_START_VALUE_H		EQU	00h


;-----------------------
;		TASTATURA		|
;-----------------------

KEY_SCHEDULE		db "987#654*321+F0C="					; -----------------------
															;|  9  |  8  |  7  |  #  |
															; -----------------------
															;|  6  |  5  |  4  |  *  |
															; -----------------------
															;|  3  |  2  |  1  |  +  |
															; -----------------------
															;|  F  |  0  |  C  |  =  |
															; -----------------------



;-------------------
;		STAMPAC		|
;-------------------

ADR_PRINTER_VIRTUAL EQU 0FF50h  ; ADRESA KOJA SA OUT KOMANDOM AKTIVIRA CSPrinter


PRINTER_BUFFER_IS_FULL db 1

PRINTER_PRINT_INDICATOR db 1 DUP(0)


PRINTER_BUFFER db 32 DUP(?)
PRINTER_BUFFER_SIZE EQU 32
PRINTER_BUFF_ITER db 1 DUP(0)
PRINTER_WAIT_TIME db 1
PRINTER_PAPER db 1


;-------------------
;		DISPLEJ		|
;-------------------

DISPLAY_BUFFER db 16 DUP(?)
DISPLAY_CURR_INDEX db 0


;-------------------
;		LADICA		|
;-------------------

ADR_LAD_VIRTUAL EQU 0FF60h  ; ADRESA KOJA SA OUT KOMANDOM AKTIVIRA CSLad
LAD_NEED_TO_OPEN db 0


;----------------------------
;      PORUKE ZA DISPLEJ	 |
;----------------------------

DISPLAY_MSG_PROGRAMIRANJE	 db	 "PROGRAMIRANJE   "
DISPLAY_MSG_FUNKCIJA		 db	 "FUNKCIJA        "
DISPLAY_MSG_CEKAM 			db	 "CEKAM           "
DISPLAY_MSG_GRESKA			db	 "GRESKA          "


;---------------------------
;		TRENUTNO VREME		|
;---------------------------

TIME_SEC	db 0
TIME_MIN	db 0
TIME_HOUR	db 0
TIME_MILLI  dw 0

;Trenutno vreme je predstavlejno u buffer-u od 8 bajtova u obliku HH:MM:SS
CURRENT_TIME db 8 DUP(':')

;-------------------------------
;		PORUKE U PROTOKOLU		|
;-------------------------------


SERIAL_TOPC_CONNECT EQU 1h
SERIAL_TOPC_DISCONNECTED EQU 2h
SERIAL_TOPC_DISCONNECT EQU 3h
SERIAL_TOPC_SEND EQU 4h

SERIAL_FROMPC_CONNECTED EQU 11h
SERIAL_FROMPC_DISCONNECT EQU 12h
SERIAL_FROMPC_DISCONNECTED EQU 13h


;---------------------------
;		BAZA PROIZVODA		|
;---------------------------

PRODUCT_NAME			db	120000 DUP(' ') ; 12 * 1000 bajtova
PRODUCT_PRICE			dw	1000  DUP(0)
PRODUCT_AMOUNT			dw	1000 DUP(0)
PRODUCTS_CNT			dw	1


CURR_PRODUCT		dw	0
CURR_PRICE			dw	0
CURR_AMOUNT		 	dw	1
CURR_BILL 			dw 	0

PROMET dw 0






DAT ENDS
;-------------------------------
;		KRAJ DATA SEGMENTA		|
;-------------------------------


;-----------------------------------
;		STACK SEGMENTA (2Kb)		|
;-----------------------------------

STACK SEGMENT AT 007C00H
	STACK_SPACE DB 2048 DUP (0)	; 
STACK ENDS

;-------------------------------
;		KRAJ STACK SEGMENTA		|
;-------------------------------


;-------------------------------
;		PROGRAM SEGMENTA		|
;		POCETNA ADRESA FF000h	|
;-------------------------------

PROG SEGMENT AT 0FF000h
ASSUME CS: PROG, DS: DAT, ES: DAT, SS: STACK

START:
	CLI				
	XOR AX, AX		
	MOV SP,	1024	; POSTAVI SP
	
	MOV AX, OFFSET intDIV_ZERO ; UCITAVNAJE OFSETA PREKIDNE RUTINE
	MOV INT_DIV_ZERO, AX	   ; SMESTANJE OFSETA U ULAZ
	mov AX, SEG intDIV_ZERO	   ; UCITAVANJE SEGMENTA PREKIDNE RUTINE
	mov INT_DIV_ZERO+2, AX	   ; SMESTANJE SEGMENTA U ULAZ
	
	MOV AX, OFFSET intTRAP
	MOV INT_TRAP, AX
	MOV AX, SEG intTRAP
	MOV INT_TRAP+2, AX
	
	MOV AX, OFFSET intNMI
	MOV INT_NMI, AX
	MOV AX, SEG intNMI
	MOV INT_NMI+2, AX
	
	MOV AX, OFFSET intCNT_1MS
	MOV INT_CNT_1MS, AX
	MOV AX, SEG intCNT_1MS
	MOV INT_CNT_1MS+2, AX
	
	MOV AX, OFFSET intCNT_20MS
	MOV INT_CNT_20MS, AX
	MOV AX, SEG intCNT_20MS
	MOV INT_CNT_20MS+2, AX
	
	MOV AX, OFFSET intKEYBOARD
	MOV INT_KEYBOARD, AX
	MOV AX, SEG intKEYBOARD
	MOV INT_KEYBOARD+2, AX
	
	MOV AX, OFFSET intPrinter
	MOV INT_PAPER, AX
	MOV AX, SEG intPrinter
	MOV INT_PAPER+2, AX
	
	MOV AX, OFFSET intSERIAL
	MOV INT_SERIAL, AX
	MOV AX, SEG intSERIAL
	MOV INT_SERIAL+2, AX
	
	;INICIJALIZACIJA PERIFERIJA
	CALL INIT_8251
	CALL INIT_8255_PRINT_KEY
	CALL INIT_8255_DISPLAY
	CALL INIT_8259
	CALL INIT_COUNTERS
	
	STI

MAIN:
	JMP MAIN ; BESKONACNA PETLJA
	
;-----------------------------------
;		KRAJ GLAVNOG PROGRAMA		|
;-----------------------------------
	

;------------------------------------------
;   INICIJALIZACIJA SERIJSKOG PORTA 8251   |
;------------------------------------------

INIT_8251 PROC NEAR
	MOV DX, ADR_8251_CTRL
	XOR AL, AL				
    OUT DX, AL
             
    ;RESET 8251   
    MOV AL, RESET_8251 
	OUT DX, AL
	
	;SETOVANJE MODA
	MOV AL, MODE_8251
	OUT DX, AL
	
	;SETOVANJE KOMANDNE RECI
	MOV AL, COMMAND_8251_STANDARD_INIT
	OUT DX, AL
	
	RET
INIT_8251 ENDP
    
;-------------------------------------------
;   INICIJALIZACIJA PARALELNOG PORTA 8255   |
;   PORT A - STAMPAC, PORT B - TASTAURA		|
;	PORT C - TASTATURA						|
;-------------------------------------------

INIT_8255_PRINT_KEY PROC NEAR
	PUSH AX
	PUSH DX
	
	MOV DX, ADR_8255_PRINTER_KEY_CTRL
	MOV AL, CTRL_8255_INIT_PRINTER_KEY
	OUT DX, AL 
	
	XOR AX, AX
	MOV DX, ADR_8255_KEYOUT
	OUT DX, AL
	
	POP AX
	POP DX
	RET
INIT_8255_PRINT_KEY ENDP

;-------------------------------------------
;   INICIJALIZACIJA PARALELNOG PORTA 8255   |
;   PORT A - DISPLEJ, PORT B - DISPLEJ		|
;-------------------------------------------

INIT_8255_DISPLAY PROC NEAR
	PUSH AX
	PUSH DX
	
	MOV DX, ADR_8255_DISPLAY_CTRL
	MOV AL, CTRL_8255_INIT_DISPLAY
	OUT DX, AL
		
	POP DX
	POP AX
	RET
INIT_8255_DISPLAY ENDP


;----------------------------------------------
;   INICIJALIZACIJA KONTROLERA PREGIDA 8259a   |
;----------------------------------------------

INIT_8259 proc near
	PUSH AX
	PUSH DX

	MOV DX, ADR_8259_DATA
	MOV AL, ICW1_8259
	OUT DX, AL
	MOV DX, ADR_8259_CTRL
	MOV AL, ICW2_8259
	OUT DX, AL
	MOV AL, ICW4_8259
	OUT DX, AL

	POP DX
	POP AX
	RET
INIT_8259 ENDP

;----------------------------------
;   INICIJALIZACIJA BROJACA 8254   |
;----------------------------------



INIT_COUNTERS proc near
	PUSH AX
	PUSH DX
	
	;INICIJALIZACIJA BROJACA 0
	MOV DX, ADR_CNT_CTRL	
	MOV AL, COUNTER_0_CTRL
	OUT DX, AL
	
	MOV DX, ADR_CNT_DATA_0
	MOV AL, COUNTER_0_START_VALUE_L
	OUT DX, AL
	MOV AL, COUNTER_0_START_VALUE_H
	OUT DX, AL
	
	;INICIJALIZACIJA BROJACA 1
	MOV DX, ADR_CNT_CTRL
	MOV AL, COUNTER_1_CTRL
	OUT DX, AL
	
	
	MOV DX, ADR_CNT_DATA_1
	MOV AL, COUNTER_1_START_VALUE_L
	OUT DX, AL
	MOV AL, COUNTER_1_START_VALUE_H
	OUT DX, AL
	
	;INICIJALIZACIJA BROJACA 2
	MOV DX, ADR_CNT_CTRL
	MOV AL, COUNTER_2_CTRL
	OUT DX, AL
	
	
	MOV DX, ADR_CNT_DATA_2
	MOV AL, COUNTER_2_START_VALUE_L
	OUT DX, AL
	MOV AL, COUNTER_2_START_VALUE_H
	OUT DX, AL
	
	POP DX
	POP AX
	RET
INIT_COUNTERS ENDP

intDIV_ZERO proc near  ;RESTARTUJE SE SISTEM AKO DODJE DO DELJENJA 0
	CALL restart
	IRET
intDIV_ZERO ENDP

intTRAP proc near  ;NE KORISTI SE
	IRET
intTRAP ENDP

intNMI proc near  ;NE KORISTI SE
	IRET
intNMI ENDP

proc printChar near		;SALJE KARAKTER STAMPACU ZA STAMPANJE
						;OCEKUJE KARAKTER U AL
	PUSH DX
	MOV DX, ADR_8255_PRINTER
	OUT DX, AL
	POP DX
	RET

printChar ENDP



PutCharToDislay PROC         ;ISPISUJE KARAKTER NA DISPLEJ
							 ;OCEKUJE KARAKTER U AL I POZICIJU U BL 
	PUSH AL
	PUSH BL
	PUSH DX
	
	CMP AL, 'A'
	JNE nextChar1
	MOV AL, 3Fh             ;DECIMALNA VREDNOST KOJA SE DEKODUJE
	JMP PutCharToDislay_putToParallel
nextChar1:
	CMP AL, 'B'
	JNE nextChar2
	MOV AL, FFh             ;DECIMALNA VREDNOST KOJA SE DEKODUJE
	JMP lblPrintScreenPutChar
nextChar2:
;   ZA OSTALE KARAKTERE ISTO

PutCharToDislay_putToParallel:
	MOV DX, ADR_8255_DISPLAY				
	OUT DX, AL
	MOV DX, ADR_DISPLAY_SELECT
	MOV AL, BL
	OUT DX, AL
	
	POP DX
	POP BL
	POP AL
	RET
PutCharToDislay ENDP




intKEYBOARD proc near	;PREKIDNA RUTINA TASTAURE
	PUSH AX
	PUSH BX
	PUSH CX
	PUSH DX
	PUSH SI
	
	
	MOV BL, FFFEh;	;	SELEKTUJE SE PRVI RED
	MOV CL, 0
	
	MOV DX, ADR_8255_KEYOUT
	MOV AL, BL
	OUT DX, AL
	
	MOV DX, ADR_8255_KEYIN
	IN AL, DX
	CMP AL, 0
	JNE intKEYBOARD_SCAN_CMPL
	
	MOV DX, ADR_8255_KEYOUT
	ROL BL                   ;	ROTIRANJE SELEKTOVANOG REDA
	MOV AL, BL
	OUT DX, AL
	
intKEYBORD_LAB_1:
	MOV DX, ADR_8255_KEYIN
	IN AL, DX
	CMP AL, 0
	JE intKEYBORD_LAB_1
	
	XOR BL, FFFFh	;ZAMENA SELEKTORA SA 0 NA 1
	
intKEYBOARD_SCAN_CMPL:   ;	U BL SE NALAZI INFROMACIJA O REDU, U AL INFORMACIJA O KOLONI
	
	AND AL, 0Fh			 
	
	;INFORMACIJA U AL I BL JE PREDSTAVLJENA TAKO DA JE JEDINICA NA POZICIJI REDA I KOLONE
	;U KOJIMA JE PRITISNUT TASTER
	;DA BI DOBILI NUMERICKE VREDNOSTI POTREBNA JE SLEDECA "AKROBACIJA"
	
	SHR BL,1
	CMP BL,4
	JNE intKEYBORD_LAB_2
	MOV BL,3
intKEYBORD_LAB_2:
	SHR AL,1
	CMP AL,4
	JNE intKEYBORD_LAB_3
	MOV AL,3
	
intKEYBORD_LAB_3:
	mov SI, AL
	mov AL, keypad[BX][SI]       ;DOHVATANJE VREDNOSTI IZ NIZA KOJI PREDSTAVLJA RASPORED DUGMICA, U AL PRITISNUT KARAKTER
	
	CLI
	
	CMP CURRENT_STATE, STATE_CEKAM
	JE LBLSTATECEKAM
	CMP CURRENT_STATE, STATE_SPREMAN
	JE LBLSTATESPREMAN
	CMP CURRENT_STATE, STATE_UNOS_STAVKI
	JE LBLSTATEUNOSSTAVKI
	CMP CURRENT_STATE, STATE_UNOS_KOLICINE
	JE LBLSTATEUNOSKOLICINE
	CMP CURRENT_STATE, STATE_RACUN
	JE LBLSTATERACUN
	CMP CURRENT_STATE, STATE_FUNKCIJA
	JE LBLSTATEFUNCTION
	JMP STATE_ERROR
	
LBLSTATECEKAM:
	CMP AL, 'F'
	JNE KRAJ
	;krece programiranje
	MOV CURRENT_STATE, STATE_PROGRAMIRANJE
	;kopiranje u displej
	LES DI, DISPLAY_BUFFER
	LDS SI, DISPLAY_MSG_PROGRAMIRANJE
	MOV CX, 15
	REP MOVS DISPLAY_BUFFER,DISPLAY_MSG_PROGRAMIRANJE
	;serijska komunikacija
	MOV AL, SERIAL_TOPC_CONNECT
	CALL SERIAL_SEND
	CALL SERIAL_RECEIVE
	;konekcija je uspostavljena
	MOV DI, offset PRODUCTS_CNT
	CALL SERIAL_RECIEVE ;prijem broja artikala
	MOV [DI], AL
	INC DI
	CALL SERIAL_RECIEVE
	MOV [DI], AL
	MOV CX, 0
	
LBLPRIJEMTARTIKALALOOP:
	MOV DI,offset PRODUCT_NAME
	MOV AX, CX
	MUL 12
	ADD DI, AX
	PUSH CX
	MOV CX,0
LBLPRIJEMIMENA:
	CALL SERIAL_RECIEVE
	MOV [DI],AL
	INC DI
	INC CX
	CMP CX, 12
	JNE LBLPRIJEMIMENA
	;sad prijem cene
	POP CX
	MOV DI, offset PRODUCT_PRICE
	MOV AL,CX
	MUL 2
	ADD DI, AX
	CALL SERIAL_RECIEVE
	MOV [DI], AL
	INC DI
	CALL SERIAL_RECIEVE
	MOV [DI], AL
	
	
	INC CX
	CMP CX, PRODUCTS_CNT
	JL LBLPRIJEMARTIKALALOOP
	;sad prijem vremena
	CALL SERIAL_RECIEVE
	MOV TIME_HOUR, AL
	CALL SERIAL_RECIEVE
	MOV TIME_MIN, AL
	CALL SERIAL_RECIEVE
	MOV TIME_SEC, AL
	CALL SERIAL_RECIEVE ;prijem diskonekcije
	MOV AL, SERIAL_TOPC_DISCONNECTED
	CALL SERIAL_SEND
	;serijska komunikacija je zavrsena
	
	MOV CURRENT_STATE, STATE_SPREMAN
	JMP KRAJ
	
LBLSTATESPREMAN:
	CMP AL, 'F'
	JE LBLUFUNKCIJU
	CMP AL, '0'
	JL LBLERRORPRELAZ
	CMP AL, '9'
	JG LBLERRORPRELAZ
	SUB AL, '0' ;sad imamo cifru u AL
	MOV CURR_PRODUCT, AL
	MOV CURR_AMOUNT, 1
	MOV SI, offset PRODUCT_PRICE
	MUL AL, 2
	ADD SI, AL
	MOV CURR_PRICE, [SI]
	MOV CURR_BILL, 0
	;formiranje stringa
	MOV CURRENT_STATE, STATE_UNOS_STAVKI
	JMP KRAJ
	
LBLUFUNKCIJU:
	MOV CURRENT_STATE, STATE_FUNKCIJA
	;string
	JMP KRAJ
	
LBLSTATEUNOSSTAVKI:
	CMP AL, 'C'
	JE LBLSTAVKEUSPREMAN
	CMP AL, '+'
	JE LBLSTAVKAURACUN
	CMP AL, '*'
	JE LBLSTAVKAUKOL
	CMP AL, '#'
	JE LBLSTAVKAURACUNNEG
	CMP AL, '0'
	JL LBLERRORPRELAZ
	CMP Al, '9'
	JG LBLERRORPRELAZ
	;cifra je
	PUSH AL
	MOV AL, CURR_PRODUCT
	MUL AL, 10
	MOV BL, AL
	POP AL
	ADD AL, BL
	MOV CURR_PRODUCT, AL
	MOV SI, offset PRODUCT_PRICE
	MUL AL, 2
	ADD SI, AL
	MOV CURR_PRICE, [SI]
	;string
	JMP KRAJ
	
	
LBLSTAVKEUSPREMAN:
	MOV CURRENT_STATE,STATE_SPREMAN
	JMP KRAJ
	
LBLSTAVKAURACUN:
	MOV AL, CURR_PRICE
	ADD CURR_BILL, AL
	MOV CURRENT_STATE, STATE_RACUN
	;string
	;stampanje
	JMP KRAJ
	
LBLSTAVKAURACUNNEG:
	MOV AL, CURR_PRICE
	SUB CURR_BILL, AL
	MOV CURRENT_STATE, STATE_RACUN
	;string
	;stampanje
	JMP KRAJ
	
LBLSTAVKAUKOL:
	MOV CURRENT_AMOUNT, 0
	MOV CURRENT_STATE, STATE_UNOS_KOLICINE
	;string
	JMP KRAJ
	
LBLSTATEUNOSKOLICINE:
	CMP AL, 'C'
	JE LBLPONISTIKOLICINU
	CMP AL, '+'
	JE LBLKOLURACUN
	CMP AL, '+'
	JE LBLKOLURACUNNEG
	CMP AL, '0'
	JL LBLERRORPRELAZ
	CMP AL, '9'
	JG LBLERRORPRELAZ
	PUSH AL
	MOV AL, CURRENT_AMOUNT
	MUL AL, 10
	MOV BL, AL
	POP AL
	ADD AL, BL
	;string
	JMP KRAJ
	
LBLPONISTIKOLICINU:
	MOV CURRENT_AMOUNT, 0
	;string
	JMP KRAJ
	
LBLKOLURACUN:
	MOV AL, CURRENT_AMOUNT
	MUL CURRENT_PRICE
	ADD CURRENT_BILL, AL
	;string
	;stampa
	MOV STANJE, STATE_RACUN
	JMP KRAJ
	
LBLKOLURACUNNEG:
	MOV AL, CURRENT_AMOUNT
	MUL CURRENT_PRICE
	SUB CURRENT_BILL, AL
	;string
	;stampa
	MOV STANJE, STATE_RACUN
	JMP KRAJ

LBLSTATERACUN:
	CMP AL, '='
	JE LBLZAVRSIRACUN
	CMP AL, '0'
	JL LBLERRORPRELAZ
	CMP AL, '9'
	JG LBLERRORPRELAZ
	MOV CURR_PRODUCT, AL
	MOV CURR_AMOUNT, 1
	MOV SI, offset PRODUCT_PRICE
	MUL AL, 2
	ADD SI, AL
	MOV CURR_PRICE, [SI]
	;formiranje stringa
	MOV CURRENT_STATE, STATE_UNOS_STAVKI
	JMP KRAJ

LBLZAVRSIRACUN:
	MOV CURRENT_STATE, STATE_SPREMAN
	;string
	JMP KRAJ
	
LBLSTATEFUNCTION:
	CMP AL, 'C'
	JE LBLFUNKCIJAUSPREMAN
	CMP AL, '0'
	JE LBLFUNKCIJASERIJSKI
	CMP AL, '1'
	JE LBLFUNKCIJASTAMPANJE
	CMP AL, '2'
	JE LBLFUNKCIJAPROMET
	CMP AL, '3'
	JE LBLFUNKCIJALADICA
	JMP LBLERRORPRELAZ
	
LBLFUNKCIJAUSPREMAN:
	MOV CURRENT_STATE, STATE_SPREMAN
	;string
	JMP KRAJ
	
LBLFUNKCIJALADICA:
	PUSH AX
	MOV AX, 1
	MOV LAD_NEED_TO_OPEN, AX
	POP AX
	
	MOV CURRENT_STATE, STATE_SPREMAN
	;string
	JMP KRAJ
	
LBLFUNKCIJAPROMET:
	;string
	MOV CURRENT_STATE, STATE_SPREMAN
	JMP KRAJ
	
LBLFUNKCIJASERIJSKI:
	MOV AL, SERIAL_TOPC_CONNECT
	CALL SERIAL_SEND
	CALL SERIAL_RECIEVE
	MOV CX, 0
	MOV DI, offset PRODUCT_AMOUNT
	
	
LBLFUNKSERLOOP:
	MOV AX, [DI]
	CMP AX, 0
	JE LBLFUNKSERSKIP
	PUSH AX
	PUSH DI
	MOV DI, offset PRODUCT_NAME
	MOV AL, CX
	MUL 12
	ADD DI, AX
	PUSH CX
	MOV CX, 0
LBLFUNKSENDNAZIV:
	MOV AL, [DI]
	CALL SERIAL_SEND
	INC CX
	INC DI
	LOOP LBLFUNKSENDNAZIV
	POP CX
	POP DI
	POP AX
	;slanje kolicine
	CALL SERIAL_SEND
	MOV AL, AH
	CALL SERIAL_SEND
	
LBLFUNKSERSKIP:	
	ADD DI, 2
	INC CX
	CMP CX, PRODUCTS_CNT
	JL LBLFUNKSERLOOP
	MOV AL, SERIAL_TOPC_DISCONNECT
	CALL SERIAL_SEND
	CALL SERIAL_RECIEVE
	MOV CURRENT_STATE, STATE_SPREMAN
	;string
	JMP KRAJ
	
LBLFUNKCIJASTAMPANJE:
	MOV CX, 0
	MOV DI, offset PRODUCT_AMOUNT
LBLFUNKSTALOOP:
	MOV AX, [DI]
	CMP AX, 0
	JE LBLFUNKSTASKIP
	PUSH AX
	PUSH DI
	MOV DI, offset PRODUCT_NAME
	MOV AL, CX
	MUL 12
	ADD DI, AX
	PUSH CX
	MOV CX, 0
LBLFUNKSTANAZIV:
	MOV AL, [DI]
	;stampa karaktera
	INC CX
	INC DI
	LOOP LBLFUNKSTANAZIV
	POP CX
	POP DI
	POP AX
	;slanje kolicine
	;stampa
	MOV AL, AH
	;stampa
	
LBLFUNKSTASKIP:	
	ADD DI, 2
	INC CX
	CMP CX, PRODUCTS_CNT
	JL LBLFUNKSTALOOP
	MOV CURRENT_STATE, STATE_SPREMAN
	;string
	JMP KRAJ
	
	
	
LBLERRORPRELAZ:
	MOV AL, CURRENT_STATE
	MOV PAST_STATE, AL
	MOV CURRENT_STATE, STATE_ERROR
	;kopiranje stringa za displej
	JMP KRAJ
	
LBLSTATEERROR:
	CMP AL, 'C'
	JNE KRAJ
	MOV AL, PAST_STATE
	MOV CURRENT_STATE, AL
	;kopiranje stringa
	JMP KRAJ
	
	
	

KRAJ:
	POP SI
	POP DX
	POP CX
	POP BX
	POP AX
	RETF
intKEYBOARD ENDP



intCNT_1MS proc near

	PUSH AX
	PUSH BX
	PUSH DX
	PUSH SI
	PUSH DI

;AZURIRAJ TRENUTNO VREME
	INC TIME_MILLI
	CMP TIME_MILLI, 1000
	JNE intCNT_LABEL_1
	
	MOV TIME_MILLI, 0 
	INC TIME_SEC
	CMP TIME_SEC, 60
	JNE intCNT_LABEL_2
	
	MOV TIME_SEC, 0
	INC TIME_MIN
	CMP TIME_MIN, 60
	JNE intCNT_LABEL_2
	
	MOV TIME_MIN, 0
	INC TIME_HOUR
	CMP TIME_HOUR, 24
	JNE intCNT_LABEL_2
	
	MOV TIME_HOUR, 0

intCNT_LABEL_2:
	;AZURIRANJE BUFFER-a, PREBACIVANJE VREDNOSTI VREMENSKIH PROMENLJIVIH U DVOBAJTNI PRIKAZ
	
	MOV SI, 0;
	MOV DI, 0;

	
	MOV AL, TIME_SEC
	MOV BL, AL
	MOV DI, 6										
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MUL BL, 10
	SUB AL, BL
	MOV disp_buff_time[DI], AL
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MOV disp_buff_time[DI+1], BL
	ADD DI, 3
	MOV AL, TIME_MIN
	MOV BL, AL
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MUL BL, 10
	SUB AL, BL
	MOV disp_buff_time[DI], AL
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MOV disp_buff_time[DI+1], BL
	ADD DI, 3
	MOV AL, TIME_HOUR
	MOV BL, AL
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MUL BL, 10
	SUB AL, BL
	MOV disp_buff_time[DI], AL
	PUSH AX
	MOV AX, BX
	DIV AX, 10
	MOV BX, AX
	POP AX
	MOV disp_buff_time[DI+1], BL
	ADD DI, 3
	
	
	;AZURIRAJ JEDAN DISPLEJ
	MOV AL, DISPLAY_CURR_INDEX
	MOV DX, ADR_8255_DISPLAY_SELECT
	OUT DX, AL
	
	MOV SI, AL
	MOV AL, DISPLAY_BUFFER[SI]  ;U AL KARAKTER
	MOV BL, SI					;U BL POZICIJA
	CALL PutCharToDislay
	INC DISPLAY_CURR_INDEX
	
	POP DI
	POP SI
	POP DX
	POP BX
	POP AX	
intCNT_1MS ENDP	

intCNT_20MS proc near
	PUSH AX
	PUSH BX
	PUSH DX
	PUSH DI
	
	STI
	MOV AL, PRINTER_PRINT_INDICATOR
	CMP AL, 1
	JNE intCNT_20MS_DO_NOT_PRINT
	DEC PRINTER_WAIT_TIME
	MOV AL, PRINTER_WAIT_TIME
	CMP AL, 0
	JNE	intCNT_20MS_DO_NOT_PRINT
	MOV AL, PRINTER_PAPER
	CMP AL, 0
	JE intCNT_20MS_DO_NOT_PRINT
	
	MOV DI, PRINTER_BUFF_ITER
	MOV AL, PRINTER_BUFFER[DI]
	MOV DX, ADR_8255_PRINTER
	OUT DX, AL
	MOV DX ADR_PRINTER_VIRTUAL
	OUT DX, AL	; IZAZIVANJE csPrinter, AL JE NEBITAN
	
	MOV BX, 1	;POSTAVLJANJE WAIT TIME-a
	CMP AL, 13  ; 13 = CR KARAKTER
	JNE intCNT_20MS_LABEL_1
	ADD BX, 9
intCNT_20MS_LABEL_1:
	
	MOV AL, PRINTER_BUFF_ITER
	INC AL
	CMP AL, PRINTER_BUFFER_SIZE
	JNE intCNT_20MS_LABEL_2
	MOV AL, 0 
	MOV PRINTER_BUFFER_IS_FULL, 0
	MOV PRINTER_PRINT_INDICATOR, 0
intCNT_20MS_LABEL_2:
	MOV PRINTER_BUFF_ITER, AL
	
	MOV AL, LAD_NEED_TO_OPEN
	CMP AL, 0
	JE intCNT_20MS_LABEL_3
	MOV DX, ADR_LAD_VIRUTAL
	OUT DX, AL
	MOV AL, 0
	MOV LAD_NEED_TO_OPEN, AL	

intCNT_20MS_LABEL_3:	
	POP DI
	POP SI
	POP DX
	POP BX
	POP AX	
	STI

intCNT_20MS ENDP

printChar proc near
	STI
	PUSH AX
	PUSH BX
	PUSH DI

	
	MOV BL, PRINTER_BUFF_ITER
	INC BL
	CMP BL, PRINTER_BUFFER_SIZE
	JNE printChar_LABEL_1
	MOV BL, 0 
printChar_LABEL_1:
	MOV PRINTER_BUFF_ITER, BL
	MOV PRINTER_PRINT_INDICATOR, 1
	MOV DI, PRINTER_BUFF_ITER
	MOV PRINTER_BUFFER[DI], AL
	
	POP DI
	POP BX
	POP AX
	
	CLI
printChar ENDP

intPrinter proc near
	STI
	PUSH AX
	MOV AX, PRINTER_PAPER
	CMP AX, 0
	JE intPrinter_LABEL_1
	MOV, AX, 0
	JMP intPrinter_LABEL_2
intPrinter_LABEL_1:
	MOV, AX, 1
intPrinter_LABEL_2:
	MOV PRINT_PAPER, 1
	POP AX
	CLI
intPrinter ENDP


SERIAL_SEND proc near
	PUSH BX
	PUSH DX
	
SERIAL_SEND_LABEL_1:
	MOV BL, SERIAL_OK_TO_SEND
	CMP BL, 1
	JNE SERIAL_SEND_LABEL_1
	
	MOV DX, ADR_8251_DATA
	OUT DX, AL
	
	POP DX
	POP BX
	
SERIAL_SEND ENDP


SERIAL_RECIEVE proc near
	PUSH BX
	PUSH DX
	
SERIAL_RECIEVE_LABEL_1:
	MOV BL, SERIAL_NEW_RCV
	CMP BL, 1
	JNE SERIAL_RECIEVE_LABEL_1
	
	MOV DX, ADR_8251_DATA
	IN AL, DX
	
	POP DX
	POP BX	

SERIAL_RECIEVE ENDP

intSERIAL
	STI
	PUSH AX
	PUSH DX
	
	MOV DX, ADR_8251_CTRL
	IN AL, DX
	AND AL, 01h
	
	CMP AL, 0
	JE intSERIAL_RCV
	MOV AL, 1
	MOV SERIAL_OK_TO_SEND, AL
	MOV AL, 0
	MOV SERIAL_NEW_RCV, AL
	JMP intSERIAL_END
		
intSERIAL_RCV:
	MOV AL, 1
	MOV SERIAL_NEW_RCV, AL
	MOV AL, 0
	MOV SERIAL_OK_TO_SEND, AL
	
intSerial_END:
	
	POP DX
	POP AX
	CLI
intSERIAL ENDP
	
	
	
	

	
	

PROG ENDS






